﻿using System;
using UnityEngine;

// Token: 0x02000149 RID: 329
[AddComponentMenu("RVP/C#/Suspension/Suspension Part", 1)]
[DisallowMultipleComponent]
[ExecuteInEditMode]
public class SuspensionPart : MonoBehaviour
{
	// Token: 0x060005F2 RID: 1522 RVA: 0x00030148 File Offset: 0x0002E348
	private void Start()
	{
		this.tr = base.transform;
		this.initialConnectPoint = this.connectPoint;
		if (this.suspension)
		{
			this.suspension.movingParts.Add(this);
			if (this.suspension.wheel)
			{
				this.wheel = this.suspension.wheel;
			}
		}
		if (this.connectObj && !this.isHub && Application.isPlaying)
		{
			this.initialDist = Mathf.Max(Vector3.Distance(this.tr.position, this.connectObj.TransformPoint(this.connectPoint)), 0.01f);
			this.initialScale = this.tr.localScale;
		}
	}

	// Token: 0x060005F3 RID: 1523 RVA: 0x0003021C File Offset: 0x0002E41C
	private void Update()
	{
		if (!Application.isPlaying)
		{
			this.tr = base.transform;
			if (this.suspension && this.suspension.wheel)
			{
				this.wheel = this.suspension.wheel;
			}
		}
		if (this.tr)
		{
			if (!this.solidAxle && ((this.suspension && !this.solidAxleConnector) || this.solidAxleConnector))
			{
				if (this.isHub && this.wheel && !this.solidAxleConnector)
				{
					if (this.wheel.rim)
					{
						this.tr.position = this.wheel.rim.position;
						this.tr.rotation = Quaternion.LookRotation(this.wheel.rim.forward, this.suspension.upDir);
						this.tr.localEulerAngles = new Vector3(this.tr.localEulerAngles.x, this.tr.localEulerAngles.y, -this.suspension.casterAngle * this.suspension.flippedSideFactor);
					}
				}
				else if (!this.isHub && this.connectObj)
				{
					this.localConnectPoint = this.connectObj.TransformPoint(this.connectPoint);
					if (this.rotate)
					{
						this.tr.rotation = Quaternion.LookRotation((this.localConnectPoint - this.tr.position).normalized, (!this.solidAxleConnector) ? this.suspension.upDir : this.tr.parent.forward);
						if (!this.solidAxleConnector)
						{
							this.tr.localEulerAngles = new Vector3(this.tr.localEulerAngles.x, this.tr.localEulerAngles.y, -this.suspension.casterAngle * this.suspension.flippedSideFactor);
						}
					}
					if (this.stretch && Application.isPlaying)
					{
						this.tr.localScale = new Vector3(this.tr.localScale.x, this.tr.localScale.y, this.initialScale.z * (Vector3.Distance(this.tr.position, this.localConnectPoint) / this.initialDist));
					}
				}
			}
			else if (this.solidAxle && this.wheel1 && this.wheel2 && this.wheel1.rim && this.wheel2.rim && this.wheel1.suspensionParent && this.wheel2.suspensionParent)
			{
				this.parentUpDir = this.tr.parent.up;
				this.wheelConnect1 = this.wheel1.rim.TransformPoint(0f, 0f, -this.wheel1.suspensionParent.pivotOffset);
				this.wheelConnect2 = this.wheel2.rim.TransformPoint(0f, 0f, -this.wheel2.suspensionParent.pivotOffset);
				this.tr.rotation = Quaternion.LookRotation(((this.wheelConnect1 + this.wheelConnect2) * 0.5f - this.tr.position).normalized, this.parentUpDir);
				this.tr.localEulerAngles = new Vector3(this.tr.localEulerAngles.x, this.tr.localEulerAngles.y, Vector3.Angle((this.wheelConnect1 - this.wheelConnect2).normalized, this.tr.parent.right) * Mathf.Sign(Vector3.Dot((this.wheelConnect1 - this.wheelConnect2).normalized, this.parentUpDir)) * Mathf.Sign(this.tr.localPosition.z) * (float)((!this.invertRotation) ? 1 : -1));
			}
		}
	}

	// Token: 0x060005F4 RID: 1524 RVA: 0x000306F8 File Offset: 0x0002E8F8
	private void OnDrawGizmosSelected()
	{
		if (!this.tr)
		{
			this.tr = base.transform;
		}
		Gizmos.color = Color.green;
		if (!this.isHub && this.connectObj && !this.solidAxle)
		{
			this.localConnectPoint = this.connectObj.TransformPoint(this.connectPoint);
			Gizmos.DrawLine(this.tr.position, this.localConnectPoint);
			Gizmos.DrawWireSphere(this.localConnectPoint, 0.01f);
		}
		else if (this.solidAxle && this.wheel1 && this.wheel2 && this.wheel1.rim && this.wheel2.rim && this.wheel1.suspensionParent && this.wheel2.suspensionParent)
		{
			this.wheelConnect1 = this.wheel1.rim.TransformPoint(0f, 0f, -this.wheel1.suspensionParent.pivotOffset);
			this.wheelConnect2 = this.wheel2.rim.TransformPoint(0f, 0f, -this.wheel2.suspensionParent.pivotOffset);
			Gizmos.DrawLine(this.wheelConnect1, this.wheelConnect2);
			Gizmos.DrawWireSphere(this.wheelConnect1, 0.01f);
			Gizmos.DrawWireSphere(this.wheelConnect2, 0.01f);
		}
	}

	// Token: 0x04000917 RID: 2327
	private Transform tr;

	// Token: 0x04000918 RID: 2328
	private Wheel wheel;

	// Token: 0x04000919 RID: 2329
	public Suspension suspension;

	// Token: 0x0400091A RID: 2330
	public bool isHub;

	// Token: 0x0400091B RID: 2331
	[Tooltip("Object to point at")]
	[Header("Connections")]
	public Transform connectObj;

	// Token: 0x0400091C RID: 2332
	[Tooltip("Local space point to point at in connectObj")]
	public Vector3 connectPoint;

	// Token: 0x0400091D RID: 2333
	[NonSerialized]
	public Vector3 initialConnectPoint;

	// Token: 0x0400091E RID: 2334
	private Vector3 localConnectPoint;

	// Token: 0x0400091F RID: 2335
	[Tooltip("Rotate to point at target?")]
	public bool rotate = true;

	// Token: 0x04000920 RID: 2336
	[Tooltip("Scale along local z-axis to reach target?")]
	public bool stretch;

	// Token: 0x04000921 RID: 2337
	private float initialDist;

	// Token: 0x04000922 RID: 2338
	private Vector3 initialScale;

	// Token: 0x04000923 RID: 2339
	[Header("Solid Axle")]
	public bool solidAxle;

	// Token: 0x04000924 RID: 2340
	public bool invertRotation;

	// Token: 0x04000925 RID: 2341
	[Tooltip("Does this part connect to a solid axle?")]
	public bool solidAxleConnector;

	// Token: 0x04000926 RID: 2342
	public Wheel wheel1;

	// Token: 0x04000927 RID: 2343
	public Wheel wheel2;

	// Token: 0x04000928 RID: 2344
	private Vector3 wheelConnect1;

	// Token: 0x04000929 RID: 2345
	private Vector3 wheelConnect2;

	// Token: 0x0400092A RID: 2346
	private Vector3 parentUpDir;
}
